
export default class UIManager {

    private static noticing = false;
    private static noticeList = [];

    private static uiList = {};


    private static openBlock() {

    }

    private static closeBlock() {

    }

    //打开窗口，窗口同一时刻可以存在多个
    public static openWindow(component, data = null, openCallback: Function = null, closeCallback: Function = null) {
        let componentName = component.name;
        let path = "prefab/" + componentName;

        //存在
        if (this.uiList['componentName']) {
            let com = this.uiList[componentName];
            if (typeof com["init"] == 'function') {
                com["init"](data);
            }
            if (typeof openCallback == 'function') {
                openCallback();
            }
        }
        //不存在
        else {
            this.openBlock();
            cc.resources.load(path, (err, prefab: cc.Prefab) => {
                if (err) {
                    console.log(err);
                    this.closeBlock();
                    return;
                }
                let content = cc.find("Canvas/Window");
                let node: cc.Node = cc.instantiate(prefab);
                node.setParent(content);

                let com = node.getComponent(componentName);

                this.uiList[componentName] = com;

                if (typeof com["init"] == 'function') {
                    com["init"](data);
                }
                if (typeof openCallback == 'function') {
                    openCallback();
                }
                if (typeof closeCallback == 'function') {
                    com['closeCallback'] = closeCallback;
                }
                this.closeBlock();
            });
        }
    }

    public static closeWindow(component) {
        let componentName = component.name;
        let com = this.uiList[componentName];
        if (com) {
            com['closeCallback'] && com['closeCallback']();
            com.node.destroy();
            this.uiList[componentName] = null;
        }
    }

    //打开通知 ，通知同一时刻只能存在一个
    public static openNotice(component, data, openCallback, closeCallback) {
        if (this.noticing) {
            let callback = () => {
                this.openNotice(component, data, openCallback, closeCallback);
            }
            this.noticeList.push(callback);
        } else {
            this.noticing = true;
            this.openWindow(component, data, openCallback, closeCallback)
        }
    }

    public static closeNotice(component) {
        this.closeWindow(component);
        this.noticing = false;
        if (this.noticeList.length > 0) {
            let callback = this.noticeList.shift();
            callback();
        }
    }


}
